using System;
using System.DirectoryServices;
using System.ServiceProcess;

namespace Sys.DirectoryServices
{
	public class WebServer
	{
        private DirectoryEntry IIS { get; set; }

        public string Server { get; set; }

        public string Status { get { return GetStatus(); } }

		public WebServer(string server)
		{
            Server = server;
		}

        public WebServer()
		{
            Server = "localhost";
		}
	
		public void Connect()
		{
            try
			{
                IIS = new DirectoryEntry("IIS://" + Server + "/W3SVC/1");                
			}
			catch (Exception e)
			{
                throw new Exception("Could not connect to " + Server, e);
			}
		}

		public void CreateVirtualDirectory(VirtualDirectory v)
		{
            string Schema = "IIsWebVirtualDir";

            DirectoryEntry root = IIS.Children.Find("Root", Schema);

			try
			{
				DirectoryEntry d = root.Children.Add(v.Name, Schema);

				d.Properties["Path"][0] = v.Path;
                d.Properties["AppFriendlyName"][0] = v.Name;
                d.Properties["AccessRead"][0] = v.IsReadable;
                d.Properties["AccessWrite"][0] = v.IsWritable;
                d.Properties["AccessExecute"][0] = v.IsExecutable;
                d.Properties["AccessScript"][0] = v.IsScriptable;
                d.Properties["EnableDirBrowsing"][0] = v.IsBrowsable;
                d.Properties["EnableDefaultDoc"][0] = v.HasDefaultDocument;
                d.Properties["DefaultDoc"][0] = v.DefaultDocument;
                d.Properties["AspEnableParentPaths"][0] = v.ASPEnabled;
                d.Properties["AuthNTLM"][0] = v.NTLMEnabled;

                d.Invoke("AppCreate", v.Security);
                d.CommitChanges();

				root.CommitChanges();
                IIS.CommitChanges();
			}
			catch (Exception e)
			{
				throw new Exception("Could not create virual directory " + v.Name, e);
			}
		}

        public void DeleteVirtualDirectory(VirtualDirectory v)
        {
            try
            {
                foreach(DirectoryEntry d in IIS.Children)
                {
                    if(d.Name == v.Name)
                    {
                        IIS.Invoke("Delete", new string[] { d.SchemaClassName, v.Name });
                        IIS.CommitChanges();
                    }
                }
            }
            catch(Exception e)
            {
                throw new Exception("Could not delete virual directory " + v.Name, e);
            }
        }

        public void Start()
        {
            ServiceController sc = new ServiceController("W3SVC");

            if (sc != null)
            {
                if (sc.Status == ServiceControllerStatus.Stopped)
                    sc.Start();
            }
        }

        public void Stop()
        {
            ServiceController sc = new ServiceController("W3SVC");

            if (sc != null)
            {
                if (sc.Status == ServiceControllerStatus.Running)
                    sc.Stop();
            }
        }

        public void Restart()
        {
            Stop();
            Start();
        }

        public string GetStatus()
        {
            string result = string.Empty;

            ServiceController sc = new ServiceController("W3SVC");

            if (sc != null)
                result = sc.Status.ToString();

            return result;
        }
	}
}